package com.sisgh.jefdv1.core.entity;

import com.sisgh.jefdv1.core.entity.field.Fields;
import com.sisgh.jefdv1.core.jdbc.table.Column;
import com.sisgh.jefdv1.core.jdbc.table.Table;
import com.sisgh.jefdv1.util.CollectionUtils;
import com.sisgh.jefdv1.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 实体工具类
 */
public class Entities {

    /**
     * Table 转换成 Entity
     * @param tables
     * @return
     */
    public static List<Entity> convertToEntities(List<Table> tables)  {
        List<Entity> entities = new ArrayList<>();

        if (CollectionUtils.isEmpty(tables)) {
            return entities;
        }

        for (Table table : tables) {
            if (table == null) {
                continue;
            }

            Entity entity = new Entity();

            String name = table.getName();
            entity.setName(StringUtils.UnderlineToUpperCamel(name));

            List<Column> columns = table.getColumns();
            entity.setFields(Fields.convertToFields(columns));

            entities.add(entity);
        }

        return entities;
    }
}
